package com.dayaokeji.rhythmschoolstudent.client.message;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;

import com.dayaokeji.imkitwrapper.fragment.CustomerChatFragment;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class CustomerMessageActivity extends BaseActivity {

    private static final String CUSTOMER_MESSAGE_FLAG = "customer_message_flag";

    private static final String EXTRA_UID = "u_id";
    private static final String EXTRA_STORE_ID = "store_id";
    private static final String EXTRA_SELLER_ID = "seller_id";

    private long currentUID;
    private long storeID;
    private long sellerID;
    private CustomerChatFragment customerChatFragment;

    public static void navigation(Context context,long uid,long storeId,long sellerId) {
        Intent intent = new Intent(context,CustomerMessageActivity.class);
        intent.putExtra(EXTRA_UID, uid);
        intent.putExtra(EXTRA_STORE_ID, storeId);
        intent.putExtra(EXTRA_SELLER_ID, sellerId);
        context.startActivity(intent);
    }

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_message);
        setSupportActionBar(toolbar);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        currentUID = intent.getLongExtra(EXTRA_UID, 0);
        storeID = intent.getLongExtra(EXTRA_STORE_ID, 0);
        sellerID = intent.getLongExtra(EXTRA_SELLER_ID, 0);
        setupView();
    }

    private void setupView() {
        customerChatFragment = (CustomerChatFragment) getSupportFragmentManager().findFragmentByTag(CUSTOMER_MESSAGE_FLAG);
        if (customerChatFragment == null) {
            customerChatFragment = CustomerChatFragment.newInstance(currentUID, storeID, sellerID);
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, customerChatFragment).commit();

        addDisposable(Observable.interval(3, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                Logger.d("refresh connect state");
                boolean b = customerChatFragment.refreshConnectState();
                if (!b) {
                    toolbar.setSubtitle("正在连接...");
                }else{
                    toolbar.setSubtitle("");
                }
            }
        }));
    }
}
